// // 登录代码
// const form = document.querySelector('form')
// const nameInp = document.querySelector('.username')
// const pwdInp = document.querySelector('.password')
// const errBox = document.querySelector('.error')

// form.addEventListener('submit', e => {
//     e.preventDefault()
//     const name = nameInp.value
//     const pwd = pwdInp.value

//     if ( !name || !pwd) return alert('请完整填写表单')
//     const xhr = new XMLHttpRequest()

//     xhr.open('POST', '/gt')
  
//     xhr.onload = function () {
//       const res = JSON.parse(xhr.responseText)
  
//       if (res.code === 0) {
//         errBox.style.display = 'block'
//         return
//       }
//       window.localStorage.setItem('id', res.user.id)
//       window.localStorage.setItem('token', res.token)
  
//       window.location.href = './index.html'
//     }
//     xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
//     xhr.send(`username=${ name }&password=${ pwd }`)
// })

const username = document.querySelector('.username')
const password = document.querySelector('.password')
$('form').on('submit',function(event){
    event.preventDefault()
    const info = {
        username:username.value,
        password:password.value
    }
    if(!info.username || !info.password) return alert('用户名密码不能为空')
    var reg = /^[a-z0-9]\w{4,11}$/
    if(!reg.test(info.username)) return alert('用户名不符合规则，请重新输入^-^')
    var reg = /\w{6,12}/
    if(!reg.test(info.password)) return alert('密码输入不符合规则，请重新输入^-^')
    $.post({
        url:'http://localhost:8888/users/login',
        data:info,
        success (e) {
            console.log(e)
            if(e.code === 1) {
                alert(e.message)
                localStorage.setItem('id',e.user.id)
                localStorage.setItem('token',e.token)
                location.href = './list.html'
            } else {
                alert('用户名或密码错误，请重新输入^-^')
            }
        }
    })
})
